import os
import platform
import uuid

from flask import current_app
from sqlalchemy import desc
from werkzeug.utils import secure_filename

from applications.exts import db
from applications.exts.init_upload import photos
from applications.models import Photo
from applications.schemas import PhotoOutSchema
from applications.common.curd import model_to_dicts

ALLOW_EXTENSIONS = set(['jpg', 'png','webp'])


# 判断文件后缀是否在列表中
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOW_EXTENSIONS


def get_photo(page, limit):
    photo = Photo.query.order_by(desc(Photo.create_time)).paginate(page=page, per_page=limit, error_out=False)
    count = Photo.query.count()
    data = model_to_dicts(schema=PhotoOutSchema, data=photo.items)
    return data, count


# 0=用户，1微信用户头像,2商品
def upload_one(photo, mime, type=0):
    if type == 0:
        print(0)
        filename = photos.save(photo)
        file_url = '/_uploads/photos/' + filename
        # file_url = photos.url(filename)
        upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
        size = os.path.getsize(upload_url + '/' + filename)
        photo = Photo(name=filename, href=file_url, mime=mime, size=size)
        db.session.add(photo)
        db.session.commit()
        print(file_url)
    elif type == 1:
        print(1)
        filename = photos.save(photo)
        file_url = '/wxavatar/' + filename
        # file_url = photos.url(filename)
        upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
        size = os.path.getsize(upload_url + '/' + filename)
        file_url = upload_url + '/' + filename
        print(file_url)
    elif type == 2:
        upload_url = current_app.config.get("UPLOADED_PHOTOS_GOODS")
        filename=upload_url + '/' + photo.name
        print(filename)
        print(2)
        # filename = photos.save(photo)
        print(22)
        file_url = '/goods/' + filename
        # file_url = photos.url(filename)

        print(upload_url)
        size = os.path.getsize(upload_url + '/' + filename)
        print(size)
        file_url = upload_url + '/' + filename
        print(2222)
        print(file_url)

    return file_url


def delete_photo_by_id(_id):
    photo_name = Photo.query.filter_by(id=_id).first().name
    photo = Photo.query.filter_by(id=_id).delete()
    db.session.commit()
    upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
    print('url:',upload_url+'/'+photo_name)
    os.remove(upload_url + '/' + photo_name)
    return photo


# 上传1头像，0商品图片
def upfile(file, flag=1):
    if flag == 1:
        UPLOAD_FOLDER = current_app.config.get("UPLOADED_PHOTOS_DEST")
    if flag == 0:
        UPLOAD_FOLDER = current_app.config.get("UPLOADED_PHOTOS_GOODS")
    # 判断文件夹是否存在，如果不存在则创建
    if not os.path.exists(UPLOAD_FOLDER):
        os.makedirs(UPLOAD_FOLDER)

    if platform.system() == "Windows":
        slash = '\\'
    else:
        slash = '/'
    print(1111)
    if file and allowed_file(file.filename):
        # secure_filename方法会去掉文件名中的中文
        filename = secure_filename(file.filename)
        # 因为上次的文件可能有重名，因此使用uuid保存文件
        # file_name = str(uuid.uuid4()) + '.' + filename.rsplit('.', 1)[1]
        file_name = str(uuid.uuid4()) + '.' + filename
        file.save(os.path.join(UPLOAD_FOLDER, file_name))
        base_path = os.getcwd()
        file_path = UPLOAD_FOLDER + slash + file_name
        print('file_path',file_path)
    return file_path